package com.datastructures.queue;

import java.util.Scanner;

/**
 * 数组实现队列
 *
 * @author MaoLin Wang
 * @date 2019/10/1719:04
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {


        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s:显示队列");
            System.out.println("e:退出");
            System.out.println("a:添加");
            System.out.println("g:取出队列数据");
            System.out.println("h：查看对头");
            key = scanner.next().charAt(0);//接收一个字符
            switch (key) {
                case 's':
                    arrayQueue.pringData();
                    break;
                case 'a':
                    arrayQueue.addQueue(scanner.nextInt());
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.println(res);

                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println(arrayQueue.headQueue());
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                default:
                    break;
            }
        }

    }
}

//使用数组模拟队列
class ArrayQueue {
    private int maxSize;//数组最大容量
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr;//存放数据。模拟队列


    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1;//指向队列头的前一个位置
        rear = -1;//指向队列尾的数据
    }

    //判断队列是否满
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    /**
     * 判断队列是否为空
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 添加数据到队列
     */
    public void addQueue(int x) {
        if (isFull()) {
            System.out.println("队列已满!");
            return;
        }
        rear++;
        arr[rear] = x;
    }

    int count=0;
    /**
     * 出队列
     */
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int temp=arr[++front];
        int head=front;
        for (int i = 0; i < arr.length-count; i++) {
            if (head<arr.length-1){

              arr[head]=arr[head+1];
                head+=1;

            }else {
                arr[head]=0;
            }
        }
        count++;
        return temp;
    }

    //打印队列
    public void pringData() {
        if (isEmpty()) {
            System.out.println("队列空");
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    public int headQueue() {
        if (isEmpty()) {
            System.out.println("队列空");
        }
        return arr[front + 1];
    }

}